var ajaxObj;
var option = 0;

/*** setups ajaxcall, fileName is url to open, cbFunction is function to call when request success, parameters is a string to send as parameters ***/
function XMLAjaxCall(fileName,cbFunction,parameters) {
    //create xhr
    if (window.ActiveXObject) {
        ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        ajaxObj = new XMLHttpRequest;
    }
    //setup xhr
    
    ajaxObj.onreadystatechange = function(){if (ajaxObj.readyState==4 && ajaxObj.status==200)
												{cbFunction(ajaxObj.responseXML);}
									}
	ajaxObj.open("POST", fileName, true);
    ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxObj.send(parameters);
}
/*****************************************************************************/
function loadOrders(inputData){
	//parameter is xml response form the XMLAjaxCall()
	var display=document.getElementById('OrderHistoryDisplay');
	var orders=inputData.getElementsByTagName('Order');
	var orderID=inputData.getElementsByTagName('OrderID');
	var orderDate=inputData.getElementsByTagName('OrderDate');
	var amount=inputData.getElementsByTagName('NetAmount');
	var status=inputData.getElementsByTagName('Status');
	
	//reset display (top half) to empty
	while(display.getElementsByTagName('*').length>0){
		var t=display.getElementsByTagName('*')[0];
		display.removeChild(t);
	}
	
	//create labels in display
	var row=document.createElement('tr');
	var col=document.createElement('td');
	col.appendChild(document.createTextNode("Order ID"));
	row.appendChild(col);
	
	col=document.createElement('td');
	col.appendChild(document.createTextNode("Order Date"));
	row.appendChild(col);
	
	col=document.createElement('td');
	col.appendChild(document.createTextNode("Amount"));
	row.appendChild(col);
	
	col=document.createElement('td');
	col.appendChild(document.createTextNode("Status"));
	row.appendChild(col);
	
	display.appendChild(row);
	
	//for each order tag found in xml	(assume that the xml is in the correct order with all the tags within)
	for(i=0;i<orders.length;i++){
		//add a row
		var newRow=document.createElement('tr');
		//insert in row orderID
		var newE=document.createElement('td');
		newE.appendChild(document.createTextNode(orderID[i].firstChild.nodeValue));
		newRow.appendChild(newE);
		
		//insert in row orderDate
		newE=document.createElement('td');			
		newE.appendChild(document.createTextNode(orderDate[i].firstChild.nodeValue));
		newRow.appendChild(newE);
		
		//insert in row amount
		newE=document.createElement('td');			
		newE.appendChild(document.createTextNode("$"+amount[i].firstChild.nodeValue));
		newRow.appendChild(newE);
		
		//insert in row the status
		newE=document.createElement('td');			
		switch(parseInt(status[i].firstChild.nodeValue)){
			case 1:
				newE.appendChild(document.createTextNode("Processed"));
				break;
			case 2:
				newE.appendChild(document.createTextNode("Dispatched"));
				break;
			case 3:
				newE.appendChild(document.createTextNode("Delivered"));
				break;
			case 9:
				newE.appendChild(document.createTextNode("Cancelled"));
				break;
		}
		/* 0 - Pending - delivery date pending accesptance 
							1 - Processed - delivery date request accepted
							2 - Dispatched - sent delivery
							3 - Delivered/Closed - delivery reached
												   signed invoice collected
							9 - Cancelled					*/
		newRow.appendChild(newE);
		display.appendChild(newRow);
		
	}
	
	//display selected order (bottom half)
	//check that selected order to display is within range
	option=option%orders.length;
	
	
	//order: for each tag, store reference in edit then change (overwrite, not append) the value in the tag
	var edit=document.getElementById('ordHistOrdID');
	edit.innerHTML="Order ID: "+orderID[option].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistCustID');
	edit.innerHTML="Cust ID: "+inputData.getElementsByTagName('CustomerID')[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistCustName');
	edit.innerHTML="Cust Name: "+inputData.getElementsByTagName('FirstName')[0].firstChild.nodeValue+" "+inputData.getElementsByTagName('LastName')[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistTotalAmount');
	edit.innerHTML="$"+parseInt(amount[option].firstChild.nodeValue).toFixed(2);
	
	edit=document.getElementById('ordHistOrdDateVal');
	edit.innerHTML=orderDate[option].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistDeliveryDateVal');
	edit.innerHTML=inputData.getElementsByTagName('DeliveryDate')[option].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistStatusValue');
	switch(parseInt(status[option].firstChild.nodeValue)){
                case 0:
                        edit.innerHTML="Pending";
			break;
		case 1:
			edit.innerHTML="Processed";
			break;
		case 2:
			edit.innerHTML="Dispatched";
			break;
		case 3:
			edit.innerHTML="Delivered";
			break;
		case 9:
			edit.innerHTML="Cancelled";
			break;
	}
	
	// fill in product list (of selected order)
	
	//initialize
	var productList=document.getElementById('ProductList');
	var count=0;
	var id=parseInt(orderID[option].firstChild.nodeValue);
	var table=inputData.getElementsByTagName('tblOrderItems');
	var table2=inputData.getElementsByTagName('Product');
	
	//reset list (ProductList: tag to display product list)
	while(productList.getElementsByTagName('tr').length>1){
		var t=productList.getElementsByTagName('tr')[1];
		productList.removeChild(t);
	}
	
	//assume that list displays quantity per unit and the total amount calculates the total
	for(i=0;i<table.length;i++){
		if(id==parseInt(table[i].getElementsByTagName('OrderID')[0].firstChild.nodeValue)){
			var pid=parseInt(table[i].getElementsByTagName('ProductID')[0].firstChild.nodeValue);
			var q=parseInt(table[i].getElementsByTagName('Quantity')[0].firstChild.nodeValue);
			var cost=parseInt(table[i].getElementsByTagName('UnitPrice')[0].firstChild.nodeValue);
			var name=""
			for(j=0;j<table2.length;j++){
				if(pid==parseInt(table2[j].getElementsByTagName('ProductID')[0].firstChild.nodeValue)){
					name=table2[j].getElementsByTagName('ItemName')[0].firstChild.nodeValue;
				}
			}
			count=count+1;
			row=document.createElement('tr');
			
			col=document.createElement('td');
			col.appendChild(document.createTextNode(count+". "+name));
			row.appendChild(col);
			
			col=document.createElement('td');
			col.appendChild(document.createTextNode(q+"Qty"));
			row.appendChild(col);
			
			col=document.createElement('td');
			col.appendChild(document.createTextNode("$"+ cost.toFixed(2) ));
			row.appendChild(col);
			
			productList.appendChild(row);
		}
	}
	
}

function nextOrder(){
	//to update the choice of orders
	option=option+1;
}

function checkOrders(inputData){
        
	//function handles orderHistory
	//initialize by referencing the tags
	var orders=inputData.getElementsByTagName('tblOrders');
	var orderID=inputData.getElementsByTagName('OrderID');
	var orderDate=inputData.getElementsByTagName('OrderDate');
	var amount=inputData.getElementsByTagName('NetAmount');
	var status=inputData.getElementsByTagName('Status');
	
	//read tags for order details into 'edit' and edit (overwrite) them
	var edit=document.getElementById('ordHistOrdID');
	edit.innerHTML="Order ID: "+orderID[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistCustID');
	edit.innerHTML="Cust ID: "+inputData.getElementsByTagName('CustomerID')[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistCustName');
	edit.innerHTML="Cust Name: "+inputData.getElementsByTagName('FirstName')[0].firstChild.nodeValue+" "+inputData.getElementsByTagName('LastName')[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistTotalAmount');
	edit.innerHTML="$"+parseInt(amount[0].firstChild.nodeValue).toFixed(2);
	
	edit=document.getElementById('ordHistOrdDateVal');
	edit.innerHTML=orderDate[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistDeliveryDateVal');
	edit.innerHTML=inputData.getElementsByTagName('DeliveryDate')[0].firstChild.nodeValue;
	
	edit=document.getElementById('ordHistStatusValue');
	switch(parseInt(status[0].firstChild.nodeValue)){
                case 0:
                        edit.innerHTML="Pending";
			break;
		case 1:
			edit.innerHTML="Processed";
			break;
		case 2:
			edit.innerHTML="Dispatched";
			break;
		case 3:
			edit.innerHTML="Delivered";
			break;
		case 9:
			edit.innerHTML="Cancelled";
			break;
	}
	
	////// fill in product list
	
	//initialize by referencing tags
	var productList=document.getElementById('ProductList');
	var count=0;
	var id=parseInt(orderID[0].firstChild.nodeValue);
	var table=inputData.getElementsByTagName('tblOrderItems');
	var table2=inputData.getElementsByTagName('Product');
	
	//reset list (productList tag)
	while(productList.getElementsByTagName('tr').length>1){
		var t=productList.getElementsByTagName('tr')[1];
		productList.removeChild(t);
	}
	
	//assume that list displays quantity per unit and the total amount calculates the total
	for(i=0;i<table.length;i++){
		if(id==parseInt(table[i].getElementsByTagName('OrderID')[0].firstChild.nodeValue)){
			var pid=parseInt(table[i].getElementsByTagName('ProductID')[0].firstChild.nodeValue);
			var q=parseInt(table[i].getElementsByTagName('Quantity')[0].firstChild.nodeValue);
			var cost=parseInt(table[i].getElementsByTagName('UnitPrice')[0].firstChild.nodeValue);
			var name=""
			for(j=0;j<table2.length;j++){
				if(pid==parseInt(table2[j].getElementsByTagName('ProductID')[0].firstChild.nodeValue)){
					name=table2[j].getElementsByTagName('ItemName')[0].firstChild.nodeValue;
				}
			}
			count=count+1;
			row=document.createElement('tr');
			
			col=document.createElement('td');
			col.appendChild(document.createTextNode(count+". "+name));
			row.appendChild(col);
			
			col=document.createElement('td');
			col.appendChild(document.createTextNode(q+"Qty"));
			row.appendChild(col);
			
			col=document.createElement('td');
			col.appendChild(document.createTextNode("$"+ cost.toFixed(2) ));
			row.appendChild(col);
			
			productList.appendChild(row);
		}
	}
	
}
/*****************************************************************************/